package com.tehui.activity.impl;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.melnykov.fab.FloatingActionButton;
import com.melnykov.fab.ScrollDirectionListener;
import com.tehui.R;
import com.tehui.activity.BaseActivity;
import com.tehui.adapter.MoodsAdapter;
import com.tehui.bean.BrandData;
import com.tehui.global.Global;
import com.tehui.utils.ui.UiUtils;

import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Response;

//人气榜
public class MoodsActivity extends BaseActivity {

    @BindView(R.id.rl)
    RecyclerView         rl;
    @BindView(R.id.swip)
    SwipeRefreshLayout   swipe;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    @BindView(R.id.llNet)
    LinearLayout         llNet;
    @BindView(R.id.btnNet)
    Button               btnNet;

    private List<BrandData.ResultBean> result;
    private LinearLayoutManager        linearLayoutManager;

    @Override
    public void initActivityData() {
        super.initActivityData();
        tvTitle.setText("人气榜");
        initData();
        swipe.setRefreshing(true);
        fab.hide(false);
        initSwipe();
        initListener();

        if (!UiUtils.isNetworkAvailable()) {
            llNet.setVisibility(View.VISIBLE);
            rl.setVisibility(View.GONE);
            swipe.setRefreshing(false);
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_moods;
    }

    private void initSwipe() {

        swipe.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light,
                android.R.color.holo_orange_light);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipe.setRefreshing(false);
                        if (!UiUtils.isNetworkAvailable()) {
                            llNet.setVisibility(View.VISIBLE);
                            rl.setVisibility(View.GONE);
                        }
                    }
                }, 1000);
            }
        });
    }

    private void initRecyclerView() {
        linearLayoutManager = new LinearLayoutManager(this);
        rl.setLayoutManager(linearLayoutManager);
        MoodsAdapter moodsAdapter = new MoodsAdapter(result);
        rl.setAdapter(moodsAdapter);
        swipe.setRefreshing(false);

        moodsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(UiUtils.getContext(), CommodityActivity.class);
                intent.putExtra("text", result.get(position).D_title);//标题
                intent.putExtra("id", result.get(position).GoodsID);//商品id
                intent.putExtra("url", result.get(position).discountLink);//优惠卷连接
                intent.putExtra("pic", result.get(position).Pic);//图
                intent.putExtra("quan", result.get(position).Quan_receive);//已领券
                intent.putExtra("num", result.get(position).Sales_num);//购买数
                intent.putExtra("time", result.get(position).Quan_time);//过期时间
                intent.putExtra("quanPrice", result.get(position).Quan_price);//优惠卷金额
                intent.putExtra("orgPrice", result.get(position).Org_Price);//原价
                intent.putExtra("price", result.get(position).Price + "");//券后价
                intent.putExtra("cid", result.get(position).Cid + "");
                intent.putExtra("IsTmall", result.get(position).IsTmall);
                intent.putExtra("Commission", result.get(position).Commission_jihua);
                intent.putExtra("quanid", result.get(position).Quan_id);
                jumpToActivity(intent);
            }
        });

        fab.attachToRecyclerView(rl, new ScrollDirectionListener() {
            @Override
            public void onScrollDown() {
                fab.hide();
            }

            @Override
            public void onScrollUp() {
                fab.show();
            }

        }, new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                RecyclerView.LayoutManager layoutManager = rl.getLayoutManager();
                if (layoutManager instanceof LinearLayoutManager) {
                    LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
                    int firstVisibleItemPosition = linearManager.findFirstVisibleItemPosition();
                    if (firstVisibleItemPosition > 5) {
                        fab.show();
                    } else {
                        fab.hide();
                    }
                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
        });
    }

    private void initListener() {
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (linearLayoutManager != null) {
                    UiUtils.MoveToPosition(linearLayoutManager);
                    fab.hide();
                }
            }
        });

        btnNet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UiUtils.isNetworkAvailable()) {
                    return;
                } else {
                    initData();
                }
            }
        });
    }

    private void initData() {
        OkGo.get(Global.URL_MOODS + 100).tag(this).cacheKey("cacheKey").cacheMode(CacheMode.DEFAULT)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (s != null) {
                            parseJson(s);
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        llNet.setVisibility(View.VISIBLE);
                        rl.setVisibility(View.GONE);
                    }
                });
    }

    //解析
    private void parseJson(String s) {
        try {
            llNet.setVisibility(View.GONE);
            rl.setVisibility(View.VISIBLE);
            BrandData brandData = JSON.parseObject(s, BrandData.class);
            result = brandData.result;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    initRecyclerView();
                }
            }, 500);
        } catch (Exception e) {
            llNet.setVisibility(View.VISIBLE);
            rl.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelAll();
    }
}

